<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div class="form-horizontal m-t" id="signupForm">
                        <!--编辑id-->
                        <input id="id" name="id" class="form-control" type="hidden">
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span style="color: red">*</span>商品类型：</label>
                            <div class="col-sm-8">
                                <select class="form-control valid" id="typeId" name="typeId">
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span style="color: red">*</span>书籍名称：</label>
                            <div class="col-sm-8">
                                <input id="bookName" name="bookName" class="form-control" type="text"
                                       placeholder="请输入类型名称">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span style="color: red">*</span>单价：</label>
                            <div class="col-sm-8">
                                <input id="price" name="price" class="form-control" type="number"
                                       placeholder="请输入书籍单价">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-8 col-sm-offset-3" style="display: flex;justify-content: space-evenly;">
                                <button class="btn btn-primary" onclick="save()">提交</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="include::footer"></div>
<!--<script src="//s.xlongwei.com/res/js/My97DatePicker/WdatePicker.js"></script>-->
<!--<script type="text/javascript" src="/js/webJs/jzweb/order/add.js">-->
<script>
    var prefix = "/jzweb/bookConfig";
    var dataList = JSON.parse(sessionStorage.getItem('bookConfig'))
    $(function () {
        getProductType()
        $('#bookName').val(dataList.bookName)
        $('#price').val(dataList.price)
        $('#id').val(dataList.id)
        $('#typeId').val(dataList.typeId)
    });
    // $.validator.setDefaults({
    //     submitHandler: function () {
    //         save();
    //     }
    // });

    function getProductType() {
        $.ajax({
            cache: true,
            type: "POST",
            url: '/jzweb/productType/list',
            data: {
                current: 1,
                size: 10000,
            },// 你的formid
            contentType: "application/x-www-form-urlencoded",
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.code == 0) {
                    $("#typeId").append('<option value="">请选择</option>');
                    for (var i = 0; i < data.data.records.length; i++) {
                        var a = data.data.records[i].typeName
                        if (data.data.records[i].state == 0) {
                            $("#typeId").append("<option value='" + data.data.records[i].id + "'>" + a + "</option>");
                        }
                    }
                } else {
                    $("#typeId").append('<option value="">暂无数据</option>');
                }
                $('#typeId').val(dataList.typeId)
            }
        });

    }


    function save() {
        if (!$('#typeId').val()) {
            layer.msg("请选择商品类型");
            return false;
        }
        if (!$('#bookName').val()) {
            layer.msg("请输入书籍名称");
            return false;
        }
        if (!$('#price').val()) {
            layer.msg("请输入书籍单价");
            return false;
        }
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/update",
            data: JSON.stringify({
                id:$('#id').val(),
                typeId: $('#typeId').val(),
                bookName: $('#bookName').val(),
                price: $('#price').val(),
            }),// 你的formid
            contentType: "application/json",
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.code == 0) {
                    parent.layer.msg("操作成功");
                    parent.reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);

                } else {
                    parent.layer.alert(data.msg)
                }

            }
        });

    }
</script>
</body>
</html>
